package com.ds.infrastructure.hyperspace.console.controller;

import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbEnvironment;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.model.request.environment.ForkListEnvironmentForm;
import com.ds.infrastructure.hyperspace.console.model.request.environment.ListEnvironmentForm;
import com.ds.infrastructure.hyperspace.console.model.request.environment.SaveEnvironmentForm;
import com.ds.infrastructure.hyperspace.console.model.request.environment.UpdateEnvironmentForm;
import com.ds.infrastructure.hyperspace.console.model.request.form.PageForm;
import com.ds.infrastructure.hyperspace.console.model.response.environment.ForkListEnvironmentVO;
import com.ds.infrastructure.hyperspace.console.model.response.environment.GetEnvironmentVO;
import com.ds.infrastructure.hyperspace.console.model.response.environment.ListEnvironmentVO;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResult;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultData;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultList;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultOk;
import com.ds.infrastructure.hyperspace.console.service.inf.IEnvironmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author: yaozhendong
 * @create: 2019-09-18 11:33
 **/

@RestController
@RequestMapping(value = "/environment", produces = {"application/json;charset=UTF-8"})
@Api(value = "Manager Environment Controller", tags = "环境管理")
public class EnvironmentController {
    @Autowired
    IEnvironmentService iEnvironmentService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存-环境")
    public JsonResultData<Long> save(@RequestBody @Valid SaveEnvironmentForm form) {
        TbEnvironment item = iEnvironmentService.selectOne(SaveEnvironmentForm.toEntity4SelectOne(form), null);
        if (item != null) {
            throw new HyperspaceException("保存环境失败，环境名称已经存在，environmentName=" + form.getEnvironmentName());
        }
        TbEnvironment entity = SaveEnvironmentForm.toEntity(form);
        iEnvironmentService.save(entity);
        return JsonResultData.build(entity.getId());
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改-环境")
    public JsonResult update(@RequestBody @Valid UpdateEnvironmentForm form) {
        int count = iEnvironmentService.update(UpdateEnvironmentForm.toEntity(form));
        if (count == 0) {
            throw new HyperspaceException("修改环境失败，没有找到对应的记录;当前id=" + form.getId());
        }
        return JsonResultOk.SUCCESS;
    }


    @PostMapping(value = "/list")
    @ApiOperation(value = "列表-环境")
    public JsonResultList<ListEnvironmentVO> list(@RequestBody @Valid ListEnvironmentForm form) {
        TbEnvironment where = ListEnvironmentForm.toEntity(form);
        TbPageWhere pageWhere = PageForm.toPageWhere(form);
        int dataTotal = iEnvironmentService.getTotal(where, null);
        if (dataTotal == 0 || pageWhere.getBegin() > dataTotal) {
            return JsonResultList.build(Collections.emptyList(), dataTotal);
        }
        List<TbEnvironment> tbEnvironmentList = iEnvironmentService.getList(where, null, pageWhere);
        List<ListEnvironmentVO> dataList = new ArrayList<>();
        tbEnvironmentList.forEach(item -> dataList.add(ListEnvironmentVO.toVO(item)));
        return JsonResultList.build(dataList, dataTotal);
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "查看-环境")
    public JsonResultData<GetEnvironmentVO> get(@RequestParam(name = "id") Long id) {
        TbEnvironment entity = iEnvironmentService.getOrThrow(id);
        GetEnvironmentVO result = GetEnvironmentVO.toVO(entity);
        return JsonResultData.build(result);
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除-环境")
    public JsonResult delete(@RequestParam(name = "id") Long id) {
        iEnvironmentService.getOrThrow(id);
        iEnvironmentService.delete(id);
        return JsonResultOk.SUCCESS;
    }

    @GetMapping(value = "/fork")
    @ApiOperation(value = "fork-环境")
    public JsonResultData<Long> fork(@RequestParam(name = "id") Long id) {
        TbEnvironment entity = iEnvironmentService.getOrThrow(id);
        Long newId = iEnvironmentService.fork(entity);
        return JsonResultData.build(newId);
    }

    @PostMapping(value = "/forkList")
    @ApiOperation(value = "Fork列表-环境")
    public JsonResultList<ForkListEnvironmentVO> forkList(@RequestParam(name = "id") Long id) {
        iEnvironmentService.getOrThrow(id);
        TbEnvironment where = ForkListEnvironmentForm.toEntity(id);
        List<TbEnvironment> tbEnvironmentList = iEnvironmentService.getList(where, null, TbPageWhere.newPage4All());
        List<ForkListEnvironmentVO> dataList = new ArrayList<>();
        tbEnvironmentList.forEach(item -> dataList.add(ForkListEnvironmentVO.toVO(item)));
        return JsonResultList.build(dataList);
    }

}
